'''
Created on Jul 8, 2010

@author: christianp
'''

from xml.dom import minidom

class XMLSeedTemplate(object):
    
    def __init__(self):
        '''Constructor'''
    
    def createXmlElements(self, tile_seeds):
        
        doc = minidom.Document()
        
        multi_seed = doc.createElement("MultiSeed")
        for seed in tile_seeds:
            tile_map = doc.createElement("TileMap")
            tilemap_title = doc.createElement("Title")
            srs = doc.createElement("SRS")
            bounding_box = doc.createElement("BoundingBox")
            tile_format = doc.createElement("TileFormat")
            tile_sets = doc.createElement("TileSets")
            
            tile_map.appendChild(tilemap_title)
            tile_map.appendChild(srs)
            tile_map.appendChild(bounding_box)
            tile_map.appendChild(tile_format)
            tile_map.appendChild(tile_sets)
            multi_seed.appendChild(tile_map)
                
            doc.appendChild(multi_seed)
            
            self.fillTileMap(tile_map, seed.href)
            self.fillElement(doc, tilemap_title, seed.tile_schema.name)
            self.fillElement(doc, srs, seed.tile_schema.src)
            
            if (seed.GetSeedExtent() != None):
                self.fillBoundingBox(bounding_box, seed.GetSeedExtent())
                
            self.fillTileFormat(tile_format, seed.tile_schema.width, seed.tile_schema.height, seed.tile_schema.format)
            if (seed.GetSeedZoomLvl() != None):
                self.fillTileSets(doc, seed.href, seed.GetSeedZoomLvl(), tile_sets)
        
        return doc
        
    def fillTileSets(self,doc, url, resolutions, tile_sets):
        for i in range(len(resolutions)):
            tile_set = doc.createElement("TileSet")
            tile_sets.appendChild(tile_set)
            #tile_set.setAttribute("href", url)
            tile_set.setAttribute("units-per-pixel", str(resolutions[i][1]))
            tile_set.setAttribute("order", str(resolutions[i][0]))
    
    def fillTileMap(self, tile_map, url):
        tile_map.setAttribute("tilemapservice", url)
    
    def fillBoundingBox(self, bounding_box, extent):
        bounding_box.setAttribute("minx", str(extent.getMinX()))
        bounding_box.setAttribute("miny", str(extent.getMinY()))
        bounding_box.setAttribute("maxx", str(extent.getMaxX()))
        bounding_box.setAttribute("maxy", str(extent.getMaxY()))
    
    def fillTileFormat(self, tile_format, width, height, extension):
        tile_format.setAttribute("width", str(width))
        tile_format.setAttribute("height", str(height))
        tile_format.setAttribute("extension", extension)
    
    def fillElement(self, doc, element, value):
        text_node = doc.createTextNode(value)
        element.appendChild(text_node)

    def createXmlDoc(self):
        return minidom.Document()
    
    def create_element(self, doc, element, description):
        return doc.createElement(description)
    
    def setAttribute(self, element, description, value):
        element.setAttribute(description, value)

    def writeXmlToFile(self, doc, location, fileName):
        xmlFile = open(location+fileName, 'w')
        doc.writexml(xmlFile)
        xmlFile.close()